﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Security.Principal;
using Logistics.Configuration;
using System.Reflection;
using DataUpload;
using System.Data;
using Logistics.DataManage;
using Logistics.DataManage.DAL;

namespace Logistics
{
    /// <summary>
    /// Interaction logic for LoginWin.xaml
    /// </summary>
    public partial class LoginWin : Window
    {
        private static int nLoginCount = 0;
        private static int MAX_LOGIN_COUNT = 5;

        public LoginWin()
        {
            InitializeComponent();

            this.TxtName.Focus();

            String CustomCompany = GlobalSetting.GetSoftWareBase("CustomCompany");
            String SoftwareName = GlobalSetting.GetSoftWareBase("SoftwareName");
            if (!String.IsNullOrEmpty(CustomCompany))
                this.txtAppName.Text = CustomCompany;
            if (!String.IsNullOrEmpty(SoftwareName))
                this.txtCopyRight.Text = SoftwareName;

            bool versionType = GlobalObjects.RegisterProduct == RegisterProduct.Required;
            String ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            //this.txtVersion.Text = (versionType ? "[验证版]版本号：" : "[非验证版]版本号：") + ver;
            this.txtVersion.Text = (versionType ? "[验证版]" : "[非验证版]");
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            String name = this.TxtName.Text;
            String password = this.TxtPassword.Password;
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请输入用户名", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            UserIdentity identity = DoLogin(name, password);
            if (identity == null)
            {
                return;
            }
            GlobalObjects.CurrentUser = identity;
            this.DialogResult = true;
            this.Close();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


        /// <summary>
        /// 登录处理
        /// </summary>
        private UserIdentity DoLogin(String name, String password)
        {
            OUser user = new OUser();
            if (name.Equals("admin") || name.Equals("maketop"))
            {
                user = UserDAO.GetUserByUserName(name);
            }
            else
            {
                user = UserDAO.IsUserExitInSite(name,GlobalObjects.GSetting.MachineOutletId);
            }
            
            if (user != null && user.UserPwd.Equals(password))
            {
                if (!user.IsUsed)
                {
                    MessageBox.Show("非常抱歉！此账号已停用", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    return null;
                }
                return FillPrincipal(user);
            }
            else
            {
                nLoginCount++;
                String logMsg = String.Format("{0}于{1}第{2}次登录失败", name, DateTime.Now, nLoginCount);

                if (nLoginCount == MAX_LOGIN_COUNT)// Over 5 times
                {
                    String msg = String.Format("第{0}次登录失败,程序将退出!", MAX_LOGIN_COUNT);
                    MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.DialogResult = false;
                    this.Close();
                }

                int n = MAX_LOGIN_COUNT - nLoginCount;
                MessageBox.Show("登录失败,您不属于此站点或者您的登录账号及密码有误!您还有" + n + "次机会", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                this.TxtName.Text = "";
                this.TxtPassword.Password = "";
                this.TxtName.Focus();
                return null;
            }
        }

        private UserIdentity FillPrincipal(OUser user)
        {
            //登录的操作日志存储
            String logInfo = String.Format("用户{0}于{1}在{2}成功登录", user.UserName, DateTime.Now, GlobalObjects.GSetting.MachineSiteName);

            UserIdentity identity = new UserIdentity(user);
            return identity;
        }
    }
}
